package com.kla.power.remote;

import com.kla.power.service.IAlarmService;
import com.kla.power.service.IInverterElectricService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * SpringBoot注册监听
 * 数采远程监听服务
 */
@Component
@Slf4j
public class CollectRemoteListen implements CommandLineRunner {

    @Resource
    private IInverterElectricService iInverterElectricService;
    @Resource
    private IAlarmService iAlarmService;

    @Override
    public void run (String... args) {
        new Thread ( () -> {
            ServerSocket server = null;
            try {
                server = new ServerSocket ( 12907 );
                while ( true ) {
                    log.info ( "监听逆变器..." );
                    Socket socket = server.accept ();
                    log.info ( "逆变器连接成功..." );
                    CollectHandler handler = new CollectHandler ( socket, iInverterElectricService, iAlarmService );
                    Thread t = new Thread ( handler );
                    t.start ();
                }
            } catch ( IOException e ) {
                e.printStackTrace ();
            }
        } ).start ();
    }
}
